package com.taobaolite;

import com.taobaolite.cart_has_items.CartHasItems;
import com.taobaolite.cart_has_items.CartRepository;
import com.taobaolite.history.History;
import com.taobaolite.history.HistoryRepository;
import com.taobaolite.item.Item;
import com.taobaolite.item.ItemRepository;
import com.taobaolite.item.ItemService;
import com.taobaolite.order.Order;
import com.taobaolite.order.OrderRepository;
import com.taobaolite.order.OrderStatus;
import com.taobaolite.order_has_items.OrderHasItems;
import com.taobaolite.order_has_items.OrderHasItemsRepository;
import com.taobaolite.tag.Tag;
import com.taobaolite.tag.TagRepository;
import com.taobaolite.transaction.Transaction;
import com.taobaolite.transaction.TransactionPK;
import com.taobaolite.transaction.TransactionRepository;
import com.taobaolite.user.User;
import com.taobaolite.user.UserRepository;
import com.taobaolite.user.UserType;
import com.taobaolite.user.admin.Admin;
import com.taobaolite.user.admin.AdminRepository;
import com.taobaolite.user.guest.Guest;
import com.taobaolite.user.guest.GuestRepository;
import com.taobaolite.user.store.Store;
import com.taobaolite.user.store.StoreRepository;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.sql.Time;
import java.sql.Timestamp;
import java.time.LocalTime;
import java.util.List;

@Configuration
public class TaobaoConfig {
    @Bean
    CommandLineRunner taobaoConfigCommandLineRunner(
            UserRepository userRepo,
            GuestRepository guestRepo,
            AdminRepository adminRepo,
            StoreRepository storeRepo,
            ItemRepository itemRepo,
            TransactionRepository transactionRepo,
            CartRepository cartRepo,
            OrderRepository orderRepo,
            HistoryRepository historyRepo,
            OrderHasItemsRepository orderHasItemsRepo,
            ItemService itemService,
            TagRepository tagRepo) {
        return args -> {
            // 新建一个Admin
            User user1 = new User("user1", "123456", 10000.0, UserType.ADMIN, "admin@gmail.com");
            Admin admin = new Admin(user1); // This implicitly calls user1.setAdmin(admin) and userRepo.save(user1)
            adminRepo.save(admin);

            // 新建一个Store
            User user2 = new User("user2","123456", 1000.0, UserType.STORE, "store@gmail.com");
            Store store = new Store(user2, "小米官方旗舰店", "10年老店", "stores/1/logo.jpg");
            storeRepo.save(store);

            // 新建一个Store
            User user2_ = new User("user2_","123456", 5430.0, UserType.STORE, "huawei@gmail.com");
            Store store2 = new Store(user2_, "华为官方旗舰店", "1000年老店", "stores/2/logo.jpg");
            storeRepo.save(store2);

            // 新建一个Guest
            User user3 = new User("user3","123456", 100.0, UserType.GUEST, "guest@email.com");
            Guest guest = new Guest(user3,"内测用户");
            guestRepo.save(guest);

            // 新建3个Item
            Item item = new Item(
                    itemService.generateItemId(47500000L),
                    "JavaEE速成教程",
                    30.2,
                    "两周时间，速成Web开发",
                    1000L,
                    store);
            itemRepo.save(item);

            Item item2 = new Item(
                    itemService.generateItemId(50000000L),
                    "小米11 Ultra",
                    5499.0,
                    "库存紧张！欲购从速！",
                    1L,
                    store);
            itemRepo.save(item2);

            Item item3 = new Item(
                    itemService.generateItemId(52000000L),
                    "华为P50",
                    7998.99,
                    "华为最新旗舰手机——华为P50！",
                    12L,
                    store2);
            itemRepo.save(item3);

            // 新建一个Order
            List<Guest> guestList = guestRepo.findAll();
            Order order1 = new Order(
                    OrderStatus.CREATED,
                    new Timestamp(System.currentTimeMillis()),
                    guestList.get(0));
            orderRepo.save(order1);

            // 加一条Transaction TODO: 改变金额 (龚嘉毅：AOP)
            Transaction transaction = new Transaction(
                    userRepo.findAll().get(0),
                    userRepo.findAll().get(1),
                    new Timestamp(System.currentTimeMillis()),
                    9.99
            );
            transactionRepo.save(transaction);

            // 加一条Transaction
            Transaction transaction2 = new Transaction(
                    userRepo.findAll().get(1),
                    userRepo.findAll().get(2),
                    new Timestamp(System.currentTimeMillis()),
                    7.82
            );
            transactionRepo.save(transaction2);

            // 加一条Transaction
            Transaction transaction3 = new Transaction(
                    userRepo.findAll().get(2),
                    userRepo.findAll().get(3),
                    new Timestamp(System.currentTimeMillis()),
                    0.01
            );
            transactionRepo.save(transaction3);

            // 在购物车里加入一条记录
            CartHasItems record = new CartHasItems(
                    itemRepo.findAll().get(0),
                    guestRepo.findAll().get(0),
                    2,
                    new Timestamp(System.currentTimeMillis())
            );
            cartRepo.save(record);

            // 加一条History
            History history = new History(
                    Timestamp.valueOf("2021-09-08 09:31:23"),
                    userRepo.findAll().get(3),
                    false,
                    itemRepo.findAll().get(0)
            );
            historyRepo.save(history);

            // 加一条OrderHasItems
            OrderHasItems orderRecord = new OrderHasItems(
                    orderRepo.findAll().get(0),
                    itemRepo.findAll().get(0),
                    12L
            );
            orderHasItemsRepo.save(orderRecord);

            // 加很多Tag
            Tag tag = new Tag("手机");
            tagRepo.save(tag);
            for (int i = 0; i < 70; i++) {
                Tag new_tag = new Tag("类别" + (i + 2));
                tagRepo.save(new_tag);
            }
        };
    }
}
